{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"$defs\": {\n",
      "    \"Point\": {\n",
      "      \"properties\": {\n",
      "        \"x\": {\n",
      "          \"title\": \"X\",\n",
      "          \"type\": \"number\"\n",
      "        },\n",
      "        \"y\": {\n",
      "          \"title\": \"Y\",\n",
      "          \"type\": \"number\"\n",
      "        },\n",
      "        \"z\": {\n",
      "          \"title\": \"Z\",\n",
      "          \"type\": \"number\"\n",
      "        }\n",
      "      },\n",
      "      \"required\": [\n",
      "        \"x\",\n",
      "        \"y\",\n",
      "        \"z\"\n",
      "      ],\n",
      "      \"title\": \"Point\",\n",
      "      \"type\": \"object\"\n",
      "    }\n",
      "  },\n",
      "  \"properties\": {\n",
      "    \"id\": {\n",
      "      \"title\": \"Id\",\n",
      "      \"type\": \"integer\"\n",
      "    },\n",
      "    \"name\": {\n",
      "      \"title\": \"Name\",\n",
      "      \"type\": \"string\"\n",
      "    },\n",
      "    \"type\": {\n",
      "      \"pattern\": \"^(\\u6212\\u6307|\\u9879\\u94fe|\\u5176\\u5b83)$\",\n",
      "      \"title\": \"Type\",\n",
      "      \"type\": \"string\"\n",
      "    },\n",
      "    \"description\": {\n",
      "      \"description\": \"\\u6240\\u6709\\u4e0e\\u8be5item\\u76f8\\u5173\\u7684\\u63cf\\u8ff0\\uff0c\\u5c3d\\u53ef\\u80fd\\u8be6\\u7ec6\\u65e0\\u9057\\u6f0f\\u3002\",\n",
      "      \"title\": \"Description\",\n",
      "      \"type\": \"string\"\n",
      "    },\n",
      "    \"number\": {\n",
      "      \"description\": \"Quantity of the item\",\n",
      "      \"title\": \"Number\",\n",
      "      \"type\": \"integer\"\n",
      "    },\n",
      "    \"price\": {\n",
      "      \"description\": \"Price of the item\",\n",
      "      \"exclusiveMaximum\": 1001.0,\n",
      "      \"exclusiveMinimum\": 0.0,\n",
      "      \"title\": \"Price\",\n",
      "      \"type\": \"number\"\n",
      "    },\n",
      "    \"position\": {\n",
      "      \"items\": {\n",
      "        \"$ref\": \"#/$defs/Point\"\n",
      "      },\n",
      "      \"title\": \"Position\",\n",
      "      \"type\": \"array\"\n",
      "    }\n",
      "  },\n",
      "  \"required\": [\n",
      "    \"id\",\n",
      "    \"name\",\n",
      "    \"type\",\n",
      "    \"description\",\n",
      "    \"number\",\n",
      "    \"price\",\n",
      "    \"position\"\n",
      "  ],\n",
      "  \"title\": \"Item\",\n",
      "  \"type\": \"object\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "from pydantic import BaseModel, Field\n",
    "from typing import List\n",
    "import json\n",
    "\n",
    "class Point(BaseModel):\n",
    "    x: float\n",
    "    y: float\n",
    "    z: float\n",
    "\n",
    "\n",
    "class Item(BaseModel):\n",
    "    id: int\n",
    "    name: str\n",
    "    type: str = Field(..., pattern=\"^(戒指|项链|其它)$\")\n",
    "    description: str = Field(..., description='所有与该item相关的描述，尽可能详细无遗漏。')\n",
    "    number: int = Field(..., description='Quantity of the item')\n",
    "    price: float = Field(..., description='Price of the item', gt=0, lt=1001)  # 限制 price 在 0 到 1000 之间\n",
    "    position: List[Point]\n",
    "\n",
    "\n",
    "# 测试代码\n",
    "schema = Item.model_json_schema()\n",
    "formatted_schema = json.dumps(schema, indent=2)\n",
    "print(formatted_schema)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 合规判定提示词 #\n",
    "from openai import OpenAI\n",
    "\n",
    "client = OpenAI(\n",
    "    #api_key=os.getenv(\"DASHSCOPE_API_KEY\"),\n",
    "    # api_key=\"sk-b571bfbe652b4ec68ac0491e33949622\", # 这种写法不好，泄露了api-key。 回头正式部署时改掉。\n",
    "    # base_url=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "    api_key=\"ollama\",\n",
    "    base_url=\"http://192.168.20.43:11434/v1\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "```json\n",
      "{\n",
      "  \"id\": 1255456,\n",
      "  \"name\": \"脚链\",\n",
      "  \"type\": \"其它\",\n",
      "  \"description\": \"这个物品是脚链，它非常受人欢迎。\",\n",
      "  \"number\": 108,\n",
      "  \"price\": 1800.7,\n",
      "  \"position\": [\n",
      "    {\n",
      "      \"x\": 1.0,\n",
      "      \"y\": 2.0,\n",
      "      \"z\": 3.0\n",
      "    }\n",
      "  ]\n",
      "}\n",
      "```\n"
     ]
    }
   ],
   "source": [
    "def llm_query_decomposition(item_desc):\n",
    "    system_prompt = f\"\"\"\n",
    "        请帮我把这个物品的描述转换成json格式的数据，\n",
    "        json scheme格式如下：\n",
    "        {Item.model_json_schema()}\n",
    "        物品描述如下：\n",
    "        {item_desc}\n",
    "        请你分析上面的描述，按照json schema，填写信息。请一定要按照json schema的格式填写，只需要输出可以被解析的json就够了，不要输出任何其它内容。\n",
    "        \"\"\"\n",
    "    completion = client.chat.completions.create(\n",
    "        model=\"qwen2-7b-instruct\",\n",
    "        messages=[{'role': 'system', 'content': system_prompt},\n",
    "                  {'role': 'user', 'content': item_desc}],\n",
    "        )\n",
    "    return completion.choices[0].message.content\n",
    "\n",
    "\n",
    "item_desc = \"\"\"\n",
    "这个物品是脚链，它非常受人欢迎，它的价格是1800.7美元，编号是1255456，现在还有108个库存，他的位置在(1.0, 2.0, 3.0)。这东西我妹妹很喜欢。\n",
    "\"\"\"\n",
    "llm_output = llm_query_decomposition(item_desc) \n",
    "print(llm_output)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
